package com.elitetyc.eliteim.client.scanner;

import com.elitetyc.eliteim.client.handler.MsgHandler;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

/**
 * @author yuanchao.tan
 * @version 1.0
 * @description 终端输入扫描器
 * @Date 2021/9/17 14:02
 */
@Component
public class TerminalScanner implements Runnable{
    private final List<MsgHandler> msgHandlerList;

    public TerminalScanner(List<MsgHandler> msgHandlerList) {
        this.msgHandlerList = msgHandlerList;
    }

    @Override
    public void run() {
        Scanner sc = new Scanner(System.in);
        while (true){
            String msg = sc.nextLine();
            for (MsgHandler msgHandler : msgHandlerList) {
                if (msgHandler.supportHandle(msg)){
                    final boolean flag = msgHandler.handleMsg(msg);
                    if (!flag){
                        break;
                    }
                }
            }
        }
    }
}
